package nju.bishe.weibo;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

import nju.bishe.weibo.adapter.UserArrayAdapter;
import nju.bishe.weibo.data.User;
import nju.bishe.weibo.ui.base.UserCursorActivity;
import weibo4android.Paging;
import weibo4android.WeiboException;

public class FollowingActivity extends UserCursorActivity {

	private static final String TAG = "FollowersActivity";
	/**代表这个是关注我的用户，用于存入数据库中*/
	private static final int SynTag = 1;
	// private ListView mUserList;
	private UserArrayAdapter mAdapter;
	private String userId;
	private static final String LAUNCH_ACTION = "nju.bishe.weibo.FOLLOWING";
	private static final String USER_ID = "userId";
	private int currentPage = 1;
	String myself = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		if (extras != null) {
			this.userId = extras.getString(USER_ID);
		} else {
			userId = TwitterApplication.getMyselfId();
		}
		Uri data = intent.getData();
		if (data != null) {
			userId = data.getLastPathSegment();
		}
		super.onCreate(savedInstanceState);

		myself = TwitterApplication.getMyselfId();
		if (getUserId() == myself) {
			setHeaderTitle("我关注的");
		}
	}

	/*
	 * 添加取消关注按钮
	 * 
	 * @see
	 * 
	 * (android.view.ContextMenu, android.view.View,
	 * android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		if (getUserId() == myself) {
			AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
			User user = getContextItemUser(info.position);
			menu.add(
					0,
					CONTENT_DEL_FRIEND,
					0,
					getResources().getString(
							R.string.cmenu_user_addfriend_prefix)
							+ user.screenName
							+ getResources().getString(
									R.string.cmenu_user_friend_suffix));

		}

	}

	public static Intent createIntent(String userId) {
		Intent intent = new Intent(LAUNCH_ACTION);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.putExtra(USER_ID, userId);
		return intent;
	}

	@Override
	public Paging getNextPage() {
		currentPage += 1;
		return new Paging(currentPage);
	}

	@Override
	protected String getUserId() {
		return this.userId;
	}

	@Override
	public Paging getCurrentPage() {

		return new Paging(this.currentPage);
	}

	@Override
	protected List<weibo4android.User> getUsers(String userId, Paging page,
			int type) throws WeiboException {
		List<weibo4android.User> users = null;
		if (type == 0) { // 刷新时候页数从0开始读
			users = getApi().getFriendsStatuses(userId, 0);
			Log.i(TAG, "write data to database"+users.size());
			getFollowerDb().syncFollowers(users,
					TwitterApplication.getMyselfId(),SynTag);
		} else {
			users = getApi().getFriendsStatuses(userId,
					20 * (page.getPage() - 1));
		}
		return users;

	}

	@Override
	protected List<nju.bishe.weibo.data.User> fetchUsersFromDb(String ownerId) {
		// TODO Auto-generated method stub
		List<nju.bishe.weibo.data.User> result = new ArrayList<nju.bishe.weibo.data.User>();
		List<weibo4android.User> users = getFollowerDb().fetchAllFollowers(
				TwitterApplication.getMyselfId(),SynTag);
		Log.i(TAG, "size of return "+users.size());
		for (weibo4android.User u : users) {
			result.add(nju.bishe.weibo.data.User.create(u));
		}
		return result;
	}

}
